package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Orderlist;
import pojo.User;
import service.UserCenterService;
import service.UserService;
import util.CommonsUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserCenter {
    @Autowired
    private UserCenterService userCenterService;
    @Autowired
    private UserService userService;
    @RequestMapping("/toCen")
    public String initCen(){return "userCenterPage";}
    @RequestMapping("/touUpdate")
    public String initUpdate(){return "userUpdateInfoPage";}
    @RequestMapping("/updateUserInfo")
    public String updateUserInfo(Model model, HttpSession session, HttpServletRequest request){
        int uid = CommonsUtils.getUserId(session);
        User user=new User();
        List<User> userList = userCenterService.sU(uid);
        model.addAttribute("userList",userList);
        String id = request.getParameter("uid");
        String pwd = request.getParameter("upwd");
        String name = request.getParameter("uname");
        String tel = request.getParameter("utel");
        String email = request.getParameter("uemail");
            user.setUid(Integer.parseInt(id));
            user.setUpwd(pwd);
            user.setUname(name);
            user.setUtel(tel);
            user.setUemail(email);
            int a = userCenterService.uU(user);
            if (a>0){
                List<User> u = userCenterService.sU(uid);
                model.addAttribute("userList",u);
                model.addAttribute("msg","用户信息修改成功");
                return "userUpdateInfoPage";
            }
        model.addAttribute("msg","用户信息修改失败");
        return "userUpdateInfoPage";
    }
    @RequestMapping("/selectMyCart")
    public String selectMyCart(Model model, HttpSession session){
        int uid = CommonsUtils.getUserId(session);
        List<pojo.Cart> cart = userCenterService.sMC(uid);
        if (cart.size()!=0){
            model.addAttribute("cart",cart);
            return "userSelectMyCartPage";
        }
        model.addAttribute("mes","您的购物车里面没有物品");
        System.out.println("您的购物车里面没有物品");
        return "userSelectMyCartPage";
    }
    @RequestMapping("/selectMyOrderList")
    public String selectMyOrderList(Model model,HttpSession session){
        int uid = CommonsUtils.getUserId(session);
        List<Orderlist> order = userCenterService.sMOL(uid);
        if (order.size()!=0){
            model.addAttribute("order",order);
            return "userSelectMyOrderListPage";
        }
        model.addAttribute("mes","您没有订单");
        System.out.println("您没有订单");
        return "userSelectMyOrderListPage";
    }
}
